﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using YT.DB ;
using YT.Commons ;
using YT.Modules ;

namespace YT.XH.NOSE
{
    public partial class frmNSMsgMtn : Form
    {
        private DBAccess mdbcon = new DBAccess(false, false);
        public bool mblnSelMsg = false;
        public frmNSMsgMtn()
        {
            InitializeComponent();
        }


        private void FillDetail()
        {
            dgvDetail.Rows.Clear(); 
            string strSQL = string.Empty;
            strSQL = "select * from mSmsMessage order by recid desc";
            DataTable dtDetail = mdbcon.GetDataTable(strSQL);
            foreach (DataRow drDtl in dtDetail.Rows)
            {
                this.dgvDetail.Rows.Add();
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColRecID.Index].Value = CvtFunc.ToString(drDtl["RecID"]);
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColSeqNo.Index].Value = this.dgvDetail.Rows.Count;
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColMsg.Index].Value = CvtFunc.ToString(drDtl["SmsMessage"]);
                this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColDesc.Index].Value = CvtFunc.ToString(drDtl["SmsDesc"]);                
            }
            dgvDetail.EndEdit(); 
        }

        private void dgvDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == ColDetail.Index)
            {
                frmNSMsgDetail frmDtl = null ;
                if (e.RowIndex < 0) return; 
                string strRecID = CvtFunc.ToString ( this.dgvDetail.Rows[e.RowIndex].Cells[ColRecID.Index].Value );
                if (strRecID == "")
                    frmDtl = new frmNSMsgDetail();
                else
                    frmDtl = new frmNSMsgDetail(strRecID);

                frmDtl.ShowDialog();
                FillDetail();
            }            
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmNSMsgDetail frmDtl = new frmNSMsgDetail();
            frmDtl.ShowDialog();            
            FillDetail();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public string SelMsg()
        {
            string strMsg = string.Empty;
            mblnSelMsg = true;
            this.dgvDetail.Columns[ColSel.Index].ReadOnly = false;
            
            this.dgvDetail.Columns[ColSel.Index].Visible = true; 
            this.ShowDialog();

            foreach (DataGridViewRow dgvRow in this.dgvDetail.Rows)
            {
                if ( CvtFunc.ToBoolean(dgvRow.Cells[ColSel.Index].Value) == true)
                {
                    strMsg = CvtFunc.ToString(dgvRow.Cells[ColMsg.Index].Value);  
                }
            }
            return strMsg;
        }

        private void frmNSMsgMtn_Load(object sender, EventArgs e)
        {
            FillDetail();
        }

        private void dgvDetail_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.dgvDetail.Rows[e.RowIndex].Cells[ColSel.Index].Value = true;
            this.Close(); 
        }

        private void dgvDetail_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0) return;
            this.dgvDetail.Rows[e.RowIndex].Cells[ColSel.Index].Value = true;
            this.Close(); 
        }

    }
}
